<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">

        <a-input placeholder="输入关键字" style="width: 180px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />
        <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>


      </div>
      <a-button type="primary" icon="plus" @click="handleCreate" v-if="!selectMode">创建{{resourceTypeText}}</a-button>
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">
      <a-table-column title="任务" width="180px">
        <template slot-scope="row">
          <img class="thumb" :src="row.thumb" />
          {{row.title}}
        </template>
      </a-table-column>
      <a-table-column title="任务类型" width="100px">
        <template slot-scope="row">
          {{ row.type | typeLists}}
        </template>
      </a-table-column>
      <a-table-column title="任务规则" width="100px">
        <template slot-scope="row">
          {{ row.rule | ruleLists}}
        </template>
      </a-table-column>
      <a-table-column title="任务单位" width="100px">
        <template slot-scope="row">
          {{ row.num}}
        </template>
      </a-table-column>
      <a-table-column title="奖励" width="100px">
        <template slot-scope="row">
          {{row.award_text}}
        </template>
      </a-table-column>
      <a-table-column title="排序" width="60px" v-if="!selectMode">
        <template slot-scope="row">
          {{row.list_weight}}
          <span @click="handleEditListWeight(row)" style="margin-left: 10px;"><a href="javascript:;">
              <a-icon type="edit" /></a></span>
        </template>
      </a-table-column>
      <a-table-column title="领取记录" width="80px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleMxLog(row.id)">领取记录</a>
        </template>
      </a-table-column>
      <a-table-column title="操作" width="100px" v-if="!selectMode">
        <template slot-scope="row">
          <span>
            <a href="javascript:;" @click="handleEdit(row)">编辑</a>
            <a-divider type="vertical" />
            <a href="javascript:;" @click="handleDelete(row)">删除</a>
          </span>
        </template>
      </a-table-column>
    </a-table>

    <a-modal
      :title="(editUuid ? '编辑' : '创建') + resourceTypeText"
      style="top: 100px;"
      :visible="isEdit"
      width="1000px"
      @cancel="isEdit = false"
      :footer="null"
    >
      <EditForm v-if="isEdit" :uuid="editUuid" @cancel="isEdit = false" @success="fetchData"></EditForm>
    </a-modal>

    <a-modal title="领取明细(默认显示7天的)" style="top: 100px;" :visible="isMxLog" width="1200px" @cancel="isMxLog = false" :footer="null">
      <Mxlog v-if="isMxLog" :task_id="task_id" @cancel="isMxLog = false" @success="fetchData"></Mxlog>
    </a-modal>
  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'
  let that
  import EditForm from "./EditTaskday.vue"
  import Mxlog from "./Mxlog.vue"
  const resourceType = 'taskday-list'
  const resourceTypeText = '每日任务'

  export default {
    components: {
      ISwitch,
      EditForm,
      Mxlog
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isMxLog: false,
        task_id: 0,
        statusList: [
          {
            text: '重复',
            key: '0'
          },
          {
            text: '每日一次',
            key: '1'
          },
        ],
        ruleList: [
          {
            title: '充值',
            value: 'recharge'
          },
          {
            title: '福袋双倍卡',
            value: 'fudai_double'
          },
        ],
        keys: {
          type: '1'
        },
        isEdit: false,
        editUuid: '',
        resourceTypeText,
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        key: '',
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      keys: {
      handler(value) {
          this.refreshData()
        },
        deep: true
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          defaultCurrent: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e.current
            this.per_page = e.pageSize
            this.fetchData()
          }
        }
      }
    },
    filters: {
      typeLists (value) {
        return that.statusList.find(item => item.key == value).text
      },
      ruleLists (value) {
        return that.ruleList.find(item => item.value == value).title
      }
    },
    beforeCreate(){
      that = this
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleMxLog(task_id){
        this.isMxLog = true;
        this.task_id = task_id;
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      updateRewardAndBrokerage(row, data) {
        this.updateItem(row, data).then(res => {
          this.$message.success('保存成功');
          this.$iForm.hide()
          this.fetchData()
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.editUuid = ""
        this.isEdit = true
      },
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      refreshData() {
        this.page = 1
        this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.fetchData()
        // clearTimeout(this.timeout)
        // this.timeout = setTimeout(() => {
        //   this.fetchData()
        // }, 300)
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })
      },
      // getCategoryList() {
      //   this.$http.get('category.list', 'article').then(res => {
      //     this.category = res.data.list
      //   })
      // },
    }
  }
</script>
<style lang="scss" scoped>
  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }
  .sort-keys {
    display: flex;
    align-items: center;

    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;
      margin-right: 50px;

      .item {
        margin-right: 30px;
        padding: 0px 10px 4px 10px;
        cursor: pointer;
        font-weight: 500;

        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
        }

        &:hover {
          color: #1890FF;
        }
      }
    }

  }


  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;
      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }
      .gender-man, .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }
      .gender-man {
        color: #409EFF;
      }
      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
